<template>
  <t-modal
    :title="title"
    :width="640"
    :visible="visible"
    :confirmLoading="loading"
    @ok="
      () => {
        $emit('ok');
      }
    "
    @cancel="
      () => {
        $emit('cancel');
      }
    "
  >
    <a-spin :spinning="loading">
      <a-form :form="form">
        <!-- 检查是否有 id 并且大于0，大于0是修改。其他是新增，新增不显示主键ID -->
        <a-form-item
          :label="$ct('name','名称')"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
              placeholder="名称"
            v-decorator="[
              'dicItemName',
              {
                rules: [
                  { required: true, message: '请输入名称！' },
                ],
              },
            ]"
          />
        </a-form-item>
        <a-form-item
          :label="$ct('dicSetCode','编码')"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <a-input
              placeholder="编码"
            v-decorator="[
              'dicItemCode',
              {
                rules: [
                  { required: true, message: '请输入编码！' },
                ],
              },
            ]"
          />
        </a-form-item>
				<a-form-item
				  :label="$ct('state','状态')"
				  :labelCol="labelCol"
				  :wrapperCol="wrapperCol"
				>
					<a-switch v-decorator="['dicItemStatus', { valuePropName: 'checked' }]" />
				</a-form-item>
				<a-form-item
				  :label="$ct('sort','排序')"
				  :labelCol="labelCol"
				  :wrapperCol="wrapperCol"
				>
				  <a-input-number
              placeholder="排序"
						:min="1"
				    v-decorator="[
				      'dicItemSort',
				      {
				        rules: [
				          { required: false, message: '请输入排序！' },
				        ],
				      },
				    ]"
				  />
				</a-form-item>
        <a-form-item class="hiddenItem">
          <a-input v-decorator="['uuid']" type="hidden" />
        </a-form-item>
        <a-form-item class="hiddenItem">
          <a-input v-decorator="['puuid']" type="hidden" />
        </a-form-item>
      </a-form>
    </a-spin>
  </t-modal>
</template>

<script>
import pick from "lodash.pick";

// 表单字段
const fields = ["uuid", "dicItemName", "dicItemCode", "dicItemSort", "dicItemStatus", "puuid"];

export default {
  props: {
    visible: {
      type: Boolean,
      required: true,
    },
    loading: {
      type: Boolean,
      default: () => false,
    },
    model: {
      type: Object,
      default: () => null,
    },
  },
  data() {
    return {
			title: "",
      labelCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 7,
        },
      },
      wrapperCol: {
        xs: {
          span: 24,
        },
        sm: {
          span: 13,
        },
      },
      form: this.$form.createForm(this)
    };
  },
  created() {
    // 防止表单未注册
    fields.forEach((v) => this.form.getFieldDecorator(v));

    // 当 model 发生改变时，为表单设置值
    this.$watch("model", () => {
      this.model && this.form.setFieldsValue(pick(this.model, fields));
    });
  },
  methods: {
    add(m) {
			this.$nextTick(() => {
			  this.form.setFieldsValue({
			    puuid: m.puuid,
			    code: m.code
			  });
			});
    },
    edit(m) {
      console.log("edit------m", m);
    }
  },
};
</script>
